﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TEdit
{
    public partial class Environments : Form
    {
        internal List<Area> Areas = new List<Area>();

        public Environments()
        {
            InitializeComponent();
        }

        internal Environments(List<Area> _areas)
        {
            InitializeComponent();
            Areas = _areas;
        }

        private void btnAddArea_Click(object sender, EventArgs e)
        {
            string areaname;
            if (textBox1.Text.Trim() != "")
                areaname = textBox1.Text.Trim();
            else
                areaname = textBox3.Text.Trim();

            if (!listBox1.Items.Contains(areaname))
            {
                listBox1.Items.Add(areaname);
                Areas.Add(new Area(areaname, textBox2.Text.Trim(), textBox3.Text.Trim(), textBox4.Text.Trim()));
            }
        }

        private void btnDelArea_Click(object sender, EventArgs e)
        {
            foreach (Area a in Areas)
            {
                if (listBox1.SelectedItem.ToString() == a.AreaName)
                {
                    Areas.Remove(a);
                    listBox1.Items.Remove(a.AreaName);
                    return;
                }
            }
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox1.SelectedItem == null)
                return;

            foreach (Area a in Areas)
            {
                if (listBox1.SelectedItem.ToString() == a.AreaName)
                {
                    textBox1.Text = a.AreaName;
                    textBox2.Text = a.Ip;
                    textBox3.Text = a.Username;
                    textBox4.Text = a.Password;
                }
            }
        }

        private void Environments_Load(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            foreach (Area a in Areas)
            {
                listBox1.Items.Add(a.AreaName);
            }
        }
    }
}
